home *** CD-ROM | disk | FTP | other *** search
- Path: druid.borland.com!usenet
- From: pete@borland.com (Pete Becker)
- Newsgroups: comp.lang.c
- Subject: Re: rand ?
- Date: 19 Mar 1996 00:39:25 GMT
- Organization: Borland International
- Message-ID: <4ikvnt$nj6@druid.borland.com>
- References: <4idjt4$4du@itsop2.its.brooklyn.cuny.edu> <314BB352.2734@ccis.com>
- NNTP-Posting-Host: pbecker.borland.com
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=ISO-8859-1
- X-Newsreader: WinVN 0.99.5
-
- In article <314BB352.2734@ccis.com>, wlund@ccis.com says...
- >
- >Daniel Zielinski wrote:
- >>
- >> I need to generate random numbers between 0 and 51, I tried rand() with
- srand(i)
- >> i changes constantly but the program never exits. looks like the random
- number
- >> is always the same. any help would be apriciated... thanss
- >> If you want a truely random number use something like this
- >
- >#define RANDOMIZE() srand(time(NULL)) /* time() is in time.h */
- >main()
- >{
- > int i ;
- > while(i != 0 )
- > printf("%d\n",i = random(52));
- >}
- >
- > int random(int i)
- > {
- > double x = RAND_MAX + 1.0 ;/* RAND_MAX is defined in stdlib.h */
- > int y ;
- > RANDOMIZE();
-
- There is rarely a need to call srand() more than once in any particular program
- run. In addition, calling it from a tight loop like this will almost certainly
- result in highly non-random results, because time() will return the same value
- in several interations of the loop, resulting in the same value being returned
- from the subsequent call to rand().
-
- > y = 1 + rand() * (i /x) ;
-
- You didn't test this, did you? The program contains an infinite loop, since the
- value of y, and therefore the return value of random, will never be 0.
-
- > return y ;
- > }
-
-